﻿package fwk.utiles 
{
	import flash.display.InteractiveObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	
	
	public class BaseButton extends EventDispatcher
	{
		
	
		protected var _mc:MovieClip;
		protected var _id:String;
		protected var _select:Boolean = false;
		protected var _over:Boolean = false;
		
		protected var _rel:String = "in" ;
		protected var _url:String = "";
		protected var _uipage:String = "";
		protected var _target:String = "_blank" ;
		protected var _clipInfoBulle:MovieClip ;
		protected var _zone:MovieClip ;
		
		
		protected var _xml:XML;
		
	
		
		public function get url():String
		{
			return _url;
		}
		public function get uipage():String
		{
			return _uipage;
		}
		public function get rel():String
		{
			return _rel;
		}
		public function get id():String
		{
			return _id;
		}
		public function set id(val:String)		{
			_id = id;;
		}
		public function get selected():Boolean
		{
			return _select;
		}
		public function set infobulle(val:MovieClip)
		{
			_clipInfoBulle = val;
			_mc.parent.addChild(_clipInfoBulle);
		}
		public function get infobulle():MovieClip
		{
			return _clipInfoBulle;
		}
		public function get clip():MovieClip
		{
			return _mc;
		}
		public function get mc():MovieClip
		{
			return _mc;
		}
		public function set xml(val:XML)
		{
			_xml = val;
			
			try { _url 	= _xml.@url; }catch (e) { };
			try { _url 	= _xml.@href; }catch (e) { };
			try { _uipage 	= _xml.@uipage; }catch (e) { };
			
			if(_xml.@rel == "out")		_rel 		= _xml.@rel;
			if(_xml.@target == "_self") _target 	= _xml.@target;
			
		}
		
		public function get xml():XML { return _xml; }
		public function BaseButton(mc:MovieClip, zone:MovieClip = null) 
		{
			_mc 		= mc;
			_zone 		= zone;
			
			init();
		}
		
		private function init() 
		{

			if (_zone == null) _zone = _mc;
			
			_mc.mouseEnabled = false;
			for (var i:int = 0; i < _mc.numChildren; i++) {
				
				if (_mc.getChildAt(i) is InteractiveObject) {
					(_mc.getChildAt(i) as InteractiveObject).mouseEnabled = false;
				}
				if (_mc.getChildAt(i) is DisplayObjectContainer) {
					(_mc.getChildAt(i) as DisplayObjectContainer).mouseChildren = false;
				}

			}
			
			_zone.mouseEnabled 		= true;
			_zone.buttonMode 		= true;
			_zone.mouseChildren 	= false;
			_zone.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverT);
			_zone.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutT);
			_zone.addEventListener(MouseEvent.CLICK, onMouseDownT);
			
		}
			
		public function select(flag:Boolean):void
		{
			if (flag) {
				_zone.enabled = false;
				_zone.mouseEnabled = false;
				if (!_over && labelExist("over", _mc)) _mc.gotoAndPlay("over");
				_over = true;
			}else {
				_zone.enabled = false;
				_zone.mouseEnabled = false;
				if(labelExist("out", _mc)) _mc.gotoAndPlay("out");
				_over = false;
			}

			_select = flag;
		}
		public function onMouseOverT(evt:MouseEvent):void
		{
			if (!_select) {
				if(labelExist("over", _mc)) _mc.gotoAndPlay("over");
				_over = true;
			}
			if (_clipInfoBulle != null) {
				if(labelExist("display", _clipInfoBulle)) _clipInfoBulle.gotoAndPlay("display");
			}
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
		}
		public function onMouseOutT(evt:MouseEvent):void
		{
			if (!_select) {
				if(labelExist("out", _mc)) _mc.gotoAndPlay("out");
				_over = false;
			}
			if (_clipInfoBulle != null) {
				if(labelExist("hide", _clipInfoBulle)) _clipInfoBulle.gotoAndPlay("hide");
			}
			
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
		}
		public function onMouseDownT(evt:MouseEvent):void
		{
				goToLink();
				dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			
		}
		private function goToLink():void
		{

				switch(_rel) {
					case "out":
						if(_url != "") navigateToURL(new URLRequest(_url), _target);
					break;
				}
		}
		
		private function labelExist(labelNameToTest:String, clip:MovieClip):Boolean
		{
			var flag:Boolean = false;
			for (var i:int = 0; i < clip.currentLabels.length; i++) {
				var label:FrameLabel = clip.currentLabels[i];
				if (labelNameToTest == label.name) {
					flag = true;
					break;
				}
			}
			
			return flag;
		}
	}
	
}